<script setup>
import { ref, onMounted, computed,watch } from 'vue'
import { useRoute } from 'vue-router'
import { useUserStore } from '@/stores/user'
const props = defineProps({
  menu: {
    require: true,
    type: Array
  }
})
const route = useRoute()
const activeIndex = computed(() => {
  return route.path
})
const userStore = useUserStore()
// 问题出现在这============不是响应式
const permissionRouter = userStore.permissionRouter.filter((item) => {
 return item.hidden != true
})


// 监听userStore的permissionRouter
watch(() => permissionRouter, (newVal) => {
  console.log(newVal);
})

const paths=['/home']
function hasPath(path) {
  return !paths.includes(path)
}
</script>

<template>
  <div class="menu-wrapper">
    <el-menu
      :default-active="activeIndex"
      class="el-menu-vertical-demo"
      router
      unique-opened
      background-color="#f8f9fa"
    >
      <template v-for="(item, index) in permissionRouter" :key="index">
        <template v-if="item.children&&hasPath(item.path)">
          <el-sub-menu :index="item.path">
            <template #title>
              <el-icon>
                <component v-if="item.meta.icon" :is="item.meta.icon" />
                <FolderAdd v-else></FolderAdd>
              </el-icon>
              <span>{{ item.meta.title }}</span>
            </template>
            <template v-for="(obj, subIndex) in item.children" :key="subIndex">
              <el-menu-item :index="`${item.path}/${obj.path}`">
                <span class="menu-item-content">
                  <span class="circleIcon"></span>
                  {{ obj.meta.title }}
                </span>
              </el-menu-item>
            </template>
          </el-sub-menu>
        </template>
        <template v-else>
          <el-menu-item :index="item.path">
            <el-icon>
              <component v-if="item.meta.icon" :is="item.meta.icon" />
              <FolderAdd v-else></FolderAdd>
            </el-icon>
            <span>{{ item.meta.title }}</span>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
    
  </div>
</template>

<style lang="scss" scoped>
.menu-wrapper {
  width: 240px;
  height: calc(100vh - 50px);
  overflow: hidden;
  overflow-x: hidden;
  background-color: #f8f9fa;
  border-right: 1px solid #eaecef;
  transition: all 0.3s ease;

  &::-webkit-scrollbar {
    width: 4px;
  }

  &::-webkit-scrollbar-thumb {
    background: #ddd;
    border-radius: 2px;
  }

  &::-webkit-scrollbar-track {
    background: #f8f9fa;
  }

  .el-menu-vertical-demo {
    width: 100%;
    height: 100%;
    border-right: none;
  }

  :deep(.el-menu) {
    border-right: none;
  }

  :deep(.el-menu-item) {
    height: 50px;
    line-height: 50px;
    margin: 4px 0;
    border-radius: 4px;
    margin-right: 12px;
    margin-left: 12px;
    width: calc(100% - 24px);
    
    &:hover {
      background-color: #e6f4ff !important;
    }

    &.is-active {
      background-color: #e6f4ff !important;
      color: #1890ff !important;
      font-weight: 500;
      
      &::before {
        content: '';
        position: absolute;
        left: 0;
        top: 0;
        width: 3px;
        height: 100%;
        background-color: #1890ff;
        border-radius: 0 2px 2px 0;
      }
    }
  }

  :deep(.el-sub-menu) {
    .el-sub-menu__title {
      height: 50px;
      line-height: 50px;
      margin: 4px 12px;
      border-radius: 4px;
      
      &:hover {
        background-color: #f0f2f5 !important;
      }
    }

    .el-menu {
      background-color: transparent;
      
      .el-menu-item {
        background-color: transparent;
        
        .menu-item-content {
          display: flex;
          align-items: center;
        }
      }
    }
  }

  .circleIcon {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background-color: #1890ff;
    margin-right: 8px;
    display: inline-block;
  }

  :deep(.el-icon) {
    font-size: 18px;
    vertical-align: middle;
    margin-right: 10px;
    color: #666;
  }
}

// 添加动画效果
:deep(.el-menu-item),
:deep(.el-sub-menu__title) {
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
</style>

